package io.github.xbeeant.eoffice.rest;

import io.github.xbeeant.eoffice.model.User;
import io.github.xbeeant.eoffice.rest.vo.CurrentUserInfo;
import io.github.xbeeant.eoffice.util.SecurityHelper;
import io.github.xbeeant.spring.security.LoginUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiaobiao
 * @version 2021/6/30
 */
@RestController
@RequestMapping("api/auth")
public class AuthRestController {

    @GetMapping("current")
    public CurrentUserInfo current(HttpServletRequest request) {
        LoginUser<User> userLoginUser = SecurityHelper.currentUser();
        if (null == userLoginUser) {
            return new CurrentUserInfo();
        }

        CurrentUserInfo currentUser = new CurrentUserInfo();
        currentUser.setAccount(userLoginUser.getUsername());
        currentUser.setUserid(userLoginUser.getUserId());
        currentUser.setName(userLoginUser.getUserNickname());
        String id = request.getSession().getId();
        currentUser.setSession(SecurityHelper.jwtToken(id));
        return currentUser;
    }
}
